/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pgpframework.authentication;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.logging.Level;
import java.util.logging.Logger;
import pgpframework.authentication.algorithm.SignatureEnum;

/**
 *
 * @author ufabc
 */
public class ApplySignatureActor {

    Signature signatureEngine;

    public ApplySignatureActor() {
        try {
            signatureEngine = Signature.getInstance(SignatureEnum.SHA256withRSA);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(ApplySignatureActor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public byte[] doApllySginature(PrivateKey privateKey, String msg) {
        byte[] signature = null;
        try {

            signatureEngine.initSign(privateKey);
       
            signature = signatureEngine.sign();

        }  catch (SignatureException ex) {
            Logger.getLogger(ApplySignatureActor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(ApplySignatureActor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return signature;
    }

    public byte[] doApllySginature(String privateKeyfilename, String msg) throws IOException {
        byte[] signature = null;
        try {

            FileInputStream fis = new FileInputStream(new File("/" + privateKeyfilename + ".privkey"));

            ByteArrayOutputStream privKeyBaos = new ByteArrayOutputStream();

            int curByte = 0;
            while ((curByte = fis.read()) != -1) {
                privKeyBaos.write(curByte);
            }

            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privKeyBaos.toByteArray());
            privKeyBaos.close();
            KeyFactory keyFactoryEngine = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactoryEngine.generatePrivate(keySpec);


            signatureEngine.initSign(privateKey);


            signatureEngine.update(msg.getBytes("UTF-8"));

            signature = signatureEngine.sign();
            fis.close();

        } catch (SignatureException ex) {
            Logger.getLogger(ApplySignatureActor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(ApplySignatureActor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeySpecException ex) {
            Logger.getLogger(ApplySignatureActor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(ApplySignatureActor.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        return signature;

    }
}
